﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Server;
using TfsExtensions.Build.Activities.Entities;
using TfsExtensions.Build.Controls;

namespace TfsExtensions.Build.Controls
{
    public partial class DialogMetadataRulesManager : Form
    {
        public Collection<AssemblySettings> MetadataCollection { get; set; }

        public string TfsServerName { get; set; }
        public VersionControlServer TfsVersionControlServer { get; set; }
        public ICommonStructureService TfsCommonStructureService { get; set; }
        

        public DialogMetadataRulesManager()
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersVisible = false;
            

            this.EnableDisableControls();
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var assemblyInfoForm = new DialogAssemblyMetadataEditor())
            {
                assemblyInfoForm.TfsVersionControlServer = this.TfsVersionControlServer;
                assemblyInfoForm.TfsCommonStructureService = this.TfsCommonStructureService;
                assemblyInfoForm.TfsServerName = this.TfsServerName;

                assemblyInfoForm.ShowDialog();
                if (assemblyInfoForm.DialogResult == DialogResult.OK)
                {
                    var details = assemblyInfoForm.Details;
                    var metadataCollection = this.MetadataCollection;
                    if (details != null)
                    {
                        if (metadataCollection == null)
                        {
                            metadataCollection = new Collection<AssemblySettings>();
                        }

                        metadataCollection.Add(details);
                        this.MetadataCollection = metadataCollection;
                    }

                    if (this.MetadataCollection != null && this.MetadataCollection.Count > 0)
                    {
                        this.dataGridView1.DataSource = null;
                        this.dataGridView1.DataSource = this.MetadataCollection;
                    }
                }
            }
            
        }

        /// <summary>
        /// Handles the Click event of the btnRemove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            var selectedRows = this.dataGridView1.SelectedRows;
            var strError = string.Empty;
            if (selectedRows.Count > 0)
            {
                var row = selectedRows[0];
                var metadataCollection = this.MetadataCollection;
                var index = row.Index;
                if (index >= metadataCollection.Count)
                {
                    strError = "Please select a filled grid row to delete";
                }
                else
                {
                    metadataCollection.RemoveAt(index);
                    this.dataGridView1.DataSource = null;
                    this.MetadataCollection = metadataCollection;
                    this.dataGridView1.DataSource = this.MetadataCollection;
                    strError = string.Empty;
                }
            }
            else
            {
                strError = "Please select a filled grid row to delete";
            }

            if (!string.IsNullOrEmpty(strError))
            {
                lblError.Visible = true;
                lblError.Text = strError;
            }
            else
            {
                lblError.Visible = false;
                lblError.Text = strError;
            }

            this.EnableDisableControls();
        }

        /// <summary>
        /// Handles the DataBindingComplete event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewBindingCompleteEventArgs"/> instance containing the event data.</param>
        void dataGridView1_DataBindingComplete(object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
        {
            this.EnableDisableControls();
        }


        /// <summary>
        /// Enables the disable controls.
        /// </summary>
        private void EnableDisableControls()
        {
            var metadataCollection = this.MetadataCollection;
            if (metadataCollection != null && metadataCollection.Count > 0)
            {
                this.btnRemove.Enabled = true;
            }
            else
            {
                this.btnRemove.Enabled = false;
            }

            if (metadataCollection != null && metadataCollection.Count > 1)
            {
                this.btnMoveDown.Enabled = true;
                this.btnMoveUp.Enabled = true;
            }
            else
            {
                this.btnMoveDown.Enabled = false;
                this.btnMoveUp.Enabled = false;
            }
        }


        /// <summary>
        /// Handles the Click event of the btnMoveUp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            var selectedRows = this.dataGridView1.SelectedRows;
            var strError = string.Empty;
            if (selectedRows.Count > 0)
            {
                var row = selectedRows[0];
                var metadataCollection = this.MetadataCollection;
                var index = row.Index;
                if (index >= metadataCollection.Count)
                {
                    strError = "Please select a filled grid row to move up or move down";
                }
                else
                {
                    if (index == 0)
                    {
                        strError = "This is first row, you can not move it up further";
                    }
                    else
                    {
                        var tempRow = metadataCollection[index];

                        var tempRow1 = metadataCollection[index - 1];

                        metadataCollection[index] = tempRow1;
                        metadataCollection[index - 1] = tempRow;

                        this.dataGridView1.DataSource = null;
                        this.MetadataCollection = metadataCollection;
                        this.dataGridView1.DataSource = this.MetadataCollection;
                        strError = string.Empty;
                    }
                }
            }
            else
            {
                strError = "Please select a filled grid row to move up or move down";
            }

            if (!string.IsNullOrEmpty(strError))
            {
                lblError.Visible = true;
                lblError.Text = strError;
            }
            else
            {
                lblError.Visible = false;
                lblError.Text = strError;
            }

            this.EnableDisableControls();
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            var selectedRows = this.dataGridView1.SelectedRows;
            var strError = string.Empty;
            if (selectedRows.Count > 0)
            {
                var row = selectedRows[0];
                var metadataCollection = this.MetadataCollection;
                var index = row.Index;
                if (index >= metadataCollection.Count)
                {
                    strError = "Please select a filled grid row to move up or move down";
                }
                else
                {
                    if (index == metadataCollection.Count - 1)
                    {
                        strError = "This is last row, you can not move it down further";
                    }
                    else
                    {
                        var tempRow = metadataCollection[index];

                        var tempRow1 = metadataCollection[index + 1];

                        metadataCollection[index] = tempRow1;
                        metadataCollection[index + 1] = tempRow;

                        this.dataGridView1.DataSource = null;
                        this.MetadataCollection = metadataCollection;
                        this.dataGridView1.DataSource = this.MetadataCollection;
                        strError = string.Empty;
                    }
                }
            }
            else
            {
                strError = "Please select a filled grid row to move up or move down";
            }

            if (!string.IsNullOrEmpty(strError))
            {
                lblError.Visible = true;
                lblError.Text = strError;
            }
            else
            {
                lblError.Visible = false;
                lblError.Text = strError;
            }

            this.EnableDisableControls();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the CellMouseMove event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellMouseEventArgs"/> instance containing the event data.</param>
        void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                var row = this.dataGridView1.Rows[rowIndex];
                var columnIndex = e.ColumnIndex;

                if (columnIndex == 3)
                {
                    //// Set tooltip for signing details
                    var signingColumn = row.Cells["signing"];
                    signingColumn.ToolTipText = row.Cells["signingDetails"].Value.ToString();
                }

                if (columnIndex == 4)
                {
                    var metadataColumn = row.Cells["metadata"];
                    metadataColumn.ToolTipText = row.Cells["metadataDetails"].Value.ToString();
                }
            }
        }

        /// <summary>
        /// Handles the CellMouseClick event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellMouseEventArgs"/> instance containing the event data.</param>
        void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.RowSelect(e.RowIndex);
        }

        /// <summary>
        /// Handles the CellMouseDoubleClick event of the dataGridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellMouseEventArgs"/> instance containing the event data.</param>
        void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.RowSelect(e.RowIndex);
        }

        /// <summary>
        /// Rows the select.
        /// </summary>
        /// <param name="rowIndex">Index of the row.</param>
        private void RowSelect(int rowIndex)
        {
            if (rowIndex >= 0)
            {
                var row = this.dataGridView1.Rows[rowIndex];
                row.Selected = true;
            }
        }

        void Form1_Shown(object sender, System.EventArgs e)
        {
            if (this.MetadataCollection != null)
            {
                this.dataGridView1.DataSource = this.MetadataCollection;
            }
        }

        
    }
}
